package com.tsintergy.tmosguide.datamng.serviceimpl.facade.impl;

import com.tsieframework.core.base.dao.jpa.query.JpaWrappers;
import com.tsieframework.core.base.service.BaseFacadeServiceImpl;
import com.tsieframework.core.base.util.TsieAssert;
import com.tsieframework.util.BeanCopierUtils;
import com.tsintergy.tmosguide.datamng.serviceapi.em.pojo.DmgEmAreaLoadFcDO;
import com.tsintergy.tmosguide.datamng.serviceapi.em.pojo.DmgEmUnitPowerFcDO;
import com.tsintergy.tmosguide.datamng.serviceapi.facade.api.DatamngFacadeService;
import com.tsintergy.tmosguide.datamng.serviceapi.facade.dto.AreaLoadFcDTO;
import com.tsintergy.tmosguide.datamng.serviceapi.facade.dto.UnitApplyPriceDTO;
import com.tsintergy.tmosguide.datamng.serviceapi.facade.dto.UnitPowerFcDTO;
import com.tsintergy.tmosguide.datamng.serviceapi.ma.pojo.DmgMaUnitApplyPriceDO;
import com.tsintergy.tmosguide.datamng.serviceimpl.em.dao.DmgEmAreaLoadFcDAO;
import com.tsintergy.tmosguide.datamng.serviceimpl.em.dao.DmgEmUnitPowerFcDAO;
import com.tsintergy.tmosguide.datamng.serviceimpl.ma.dao.DmgMaUnitApplyPriceDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author longyz
 */
@Service("datamngFacadeService")
public class DatamngFacadeServiceImpl extends BaseFacadeServiceImpl implements DatamngFacadeService {

    @Autowired
    DmgEmUnitPowerFcDAO dmgEmUnitPowerFcDAO;

    @Autowired
    DmgEmAreaLoadFcDAO areaLoadFcDAO;

    @Autowired
    DmgMaUnitApplyPriceDAO dmgMaUnitApplyPriceDAO;

    @Override
    public List<UnitPowerFcDTO> queryUnitPowerFcList(List<String> unitIdList, Date date) {
        TsieAssert.notNull(date, "请填写日期");
        List<DmgEmUnitPowerFcDO> unitApplyPriceDOList = dmgEmUnitPowerFcDAO.findAll(
                JpaWrappers.<DmgEmUnitPowerFcDO>lambdaQuery()
                        .eq(DmgEmUnitPowerFcDO::getDate, date)
                        .in(CollectionUtils.isNotEmpty(unitIdList), DmgEmUnitPowerFcDO::getUnitId, unitIdList)
        );

        return BeanCopierUtils.copyPropertiesOfList(unitApplyPriceDOList, UnitPowerFcDTO.class);
    }

    @Override
    public AreaLoadFcDTO queryAreaLoadFcDTO(Date date) {
        TsieAssert.notNull(date, "请填写日期");
        DmgEmAreaLoadFcDO areaLoadFcDO = areaLoadFcDAO.findOne(
                JpaWrappers.<DmgEmAreaLoadFcDO>lambdaQuery()
                        .eq(DmgEmAreaLoadFcDO::getDate, date)
        );
        return areaLoadFcDO != null ? BeanCopierUtils.copyProperties(areaLoadFcDO, AreaLoadFcDTO.class) : null;
    }

    @Override
    public List<UnitApplyPriceDTO> queryUnitApplyPriceList(List<String> unitIdList, Date date) {
        //TODO 仿照queryUnitPowerFcList方法实现此功能，操作步骤：
        //1）注入DmgMaUnitApplyPriceDAO
        //2）验证日前
        //3）使用unitApplyPriceDAO的findAll方法根据unitIdList和date查询数据
        //4）使用BeanCopierUtils复制值到UnitApplyPriceDTO并返回
        TsieAssert.notNull(date,"请填写日期");
        List<DmgMaUnitApplyPriceDO> unitApplyPriceDOList = dmgMaUnitApplyPriceDAO.findAll(
                JpaWrappers.<DmgMaUnitApplyPriceDO>lambdaQuery()
                        .eq(DmgMaUnitApplyPriceDO::getDate, date)
                        .in(CollectionUtils.isNotEmpty(unitIdList),DmgMaUnitApplyPriceDO::getUnitId, unitIdList)
        );
        return BeanCopierUtils.copyPropertiesOfList(unitApplyPriceDOList, UnitApplyPriceDTO.class);
    }

}
